package com.quyunshuo.module.home.fragment.collection

import android.util.Log
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.quyunshuo.androidbaseframemvvm.base.ktx.observeLiveData
import com.quyunshuo.androidbaseframemvvm.base.ktx.visible
import com.quyunshuo.androidbaseframemvvm.common.ui.BaseFragment
import com.quyunshuo.androidbaseframemvvm.common.utils.ToastUtil
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleBean
import com.quyunshuo.androidbaseframemvvm.network.bean.ArticleListRes2
import com.quyunshuo.module.home.databinding.HomeFragmentCollectionBinding
import com.quyunshuo.module.home.fragment.CallErrors
import com.quyunshuo.module.home.fragment.HomeState
import com.quyunshuo.module.home.fragment.Result
import com.quyunshuo.module.home.fragment.video.ViedeoAdapter
import com.scwang.smart.refresh.layout.api.RefreshLayout
import com.scwang.smart.refresh.layout.listener.OnRefreshLoadMoreListener
import dagger.hilt.android.AndroidEntryPoint

/**
 * @author DBoy 2021/7/6 <p>
 * - 文件描述 : 测试fragment
 */
@AndroidEntryPoint
class CollectionFragment : BaseFragment<HomeFragmentCollectionBinding, CollectionViewModel>(),
    OnRefreshLoadMoreListener {

    override val mViewModel by viewModels<CollectionViewModel>()

    override fun createVB() = HomeFragmentCollectionBinding.inflate(layoutInflater)
    private lateinit var adapter: ViedeoAdapter
    private val dataList = mutableListOf<ArticleBean>()
    private var page = 0
    private var isRefresh = true;//true是刷新 false是累加

    override fun HomeFragmentCollectionBinding.initView() {
        recyclerView.layoutManager = LinearLayoutManager(context)
        adapter = ViedeoAdapter(dataList)
        recyclerView.adapter = adapter
        mBinding?.smartRefreshLayout?.apply {
            autoRefresh()
            setOnRefreshLoadMoreListener(this@CollectionFragment)
        }
    }

    private fun loadData() {
        mViewModel.dispatchIntent(CollectionIntent.SearchCollections(page))
    }

    override fun initObserve() {
//        observeLiveData(mViewModel.firstData1,::videoDomeIntent)
        observeLiveData(mViewModel.firstData2,::videoDomeIntent2)
    }

    private fun videoDomeIntent2(result: Result<ArticleListRes2>?) {
        when (result) {
            is Result.Error -> {
                errorView(result.exception)
            }
            is Result.Loading -> {}
            is Result.Success -> {
                videoDome(result.data)

            }
            else -> {}
        }
    }

    private fun videoDomeIntent(state: HomeState?) {
        when (state) {
            is HomeState.Exception->{
                errorView(state.callErrors)
            }
            HomeState.Loading -> {}
            is HomeState.ResultAllPersona -> {
                videoDome(state.data)
            }
            else -> {}
        }
    }

    private fun errorView(s: CallErrors?) {
        mBinding.emptyDataView.visible()
        mBinding.smartRefreshLayout.finishRefreshWithNoMoreData()
        Log.d("errorView", "errorView: ")
    }


    private fun videoDome(articleListRes2: ArticleListRes2?) {
        val datas = articleListRes2?.getDatas()
        if (isRefresh) {
            dataList.clear()
            if (datas!!.isEmpty()){
                ToastUtil.showToast("数据为空")
            }
            // 添加模拟数据
            articleListRes2?.getDatas()!!.forEach{dataList.add(it)}
            mBinding.smartRefreshLayout.finishRefresh()
        } else {
            if (datas!!.isEmpty()){
                ToastUtil.showToast("数据已加载完毕")
            }
            articleListRes2?.getDatas()!!.forEach{dataList.add(it)}
            mBinding.smartRefreshLayout.finishLoadMore()

        }
        adapter.notifyDataSetChanged()
    }

    override fun initRequestData() {
    }

    override fun onRefresh(refreshLayout: RefreshLayout) {
        page = 0
        isRefresh = true
        loadData()
    }

    override fun onLoadMore(refreshLayout: RefreshLayout) {
        page++
        isRefresh = false
        loadData()
    }

    override fun onDestroyView() {
        mBinding.recyclerView.adapter = null
        super.onDestroyView()
    }
}